package com.example.as_sum.glide;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

import java.io.File;

/**
 * Created by ran on 2016/8/29.
 */
public class GlideUtil {

    public interface LoadCallback {
        void LoadComplete();
    }

    /**
     * 加载一般的图片
     *
     * @param object 图片所在的对象 Activity，FragmentActivity，Fragment，Context
     * @param type   图片类型 File Uri,String,int
     * @param img    目标
     */
    public static void loadImage(Object object, Object type, ImageView img) {
        initRequestType(object, type)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(img);
    }

    /**
     * 加载fullsize图片
     *
     * @param object 图片所在的对象 Activity，FragmentActivity，Fragment，Context
     * @param type   图片类型 File Uri,String,int
     * @param rid    占位资源id
     * @param img    目标
     */
    public static void loadFullSizeImage(Object object, Object type, int rid, final ImageView img, final int screenWidth, final LoadCallback callback) {
        initRequestType(object, type)
                .asBitmap().into(new BitmapImageViewTarget(img) {
            @Override
            protected void setResource(Bitmap resource) {
                int w = resource.getWidth();
                int h = resource.getHeight();
                ViewGroup.LayoutParams params = img.getLayoutParams();
                int height = screenWidth * h / w;
                params.height = height;
//                Logger.e("  width " + w + "  height  " + h + "  screenW  " + screenWidth + " screenH  " + height);
                img.setLayoutParams(params);
                img.setImageBitmap(resource);
                if (callback != null)
                    callback.LoadComplete();
                super.setResource(resource);
            }
        });

    }

    /**
     * 加载一般的图片
     *
     * @param object 图片所在的对象 Activity，FragmentActivity，Fragment，Context
     * @param type   图片类型 File Uri,String,int
     * @param rid    占位资源id
     * @param img    目标
     */
    public static void loadImage(Object object, Object type, int rid, ImageView img) {
        initRequestType(object, type)
                .placeholder(rid)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(img);
        //DiskCacheStrategy.RESULT表示保存你操作以后的图片对象，你可以对原图进行操作(例如转换)
    }

    /**
     * 加载圆型图片
     *
     * @param context 加载圆的时候需要
     * @param object  图片所在的对象 Activity，FragmentActivity，Fragment，Context
     * @param type    图片类型 File Uri,String,int
     * @param img     目标
     */
    public static void loadCircleImage(Context context, Object object, Object type, ImageView img) {
        initRequestType(object, type)
                .transform(new CircleTransform(context))
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(img);
    }

    /**
     * 加载圆型图片
     *
     * @param context 加载圆的时候需要
     * @param object  图片所在的对象 Activity，FragmentActivity，Fragment，Context
     * @param type    图片类型 File Uri,String,int
     * @param img     目标
     */
    public static void loadCircleImage(Context context, Object object, Object type, int rid, ImageView img) {
        initRequestType(object, type)
                .placeholder(rid)
                .transform(new CircleTransform(context))
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(img);
    }

    /**
     * 清除内存
     *
     * @param context
     */
    public static void clearMemory(Context context) {
        Glide.get(context).clearMemory();
    }

    /**
     * 清除硬盘的缓存(新开线程执行)
     *
     * @param context
     */
    public static void clearDiskCache(Context context) {
        Glide.get(context).clearDiskCache();
    }

    public static RequestManager initRequestManager(Object object) {
        RequestManager manager = null;
        if (object instanceof Activity) {
            manager = Glide.with((Activity) object);
        } else if (object instanceof android.app.Fragment) {
            manager = Glide.with((android.app.Fragment) object);
        } else if (object instanceof FragmentActivity) {
            manager = Glide.with((FragmentActivity) object);
        } else if (object instanceof Fragment) {
            manager = Glide.with((Fragment) object);
        } else if (object instanceof Context) {
            manager = Glide.with((Context) object);
        }
        return manager;
    }

    public static DrawableTypeRequest initRequestType(Object object, Object type) {
        if (type instanceof File) {
            DrawableTypeRequest<File> request = initRequestManager(object).load((File) type);
            return request;
        } else if (type instanceof Uri) {
            DrawableTypeRequest<Uri> request = initRequestManager(object).load((Uri) type);
            return request;
        } else if (type instanceof String) {
            DrawableTypeRequest<String> request = initRequestManager(object).load((String) type);
            return request;
        } else if (type instanceof Integer) {
            DrawableTypeRequest<Integer> request = initRequestManager(object).load((Integer) type);
            return request;
        } else {
            throw new RuntimeException();
        }
    }
}
